package loge

import (
	"fmt"
	"os"

	"log"
	"time"
	"net/http"
)

var ShowConsole = false
var logPath string

func getLogFileName() string {
	return time.Now().Format("/log_2006-01-02") + ".log"
}
func ShowLog(pattern string) {
	http.Handle(pattern+"/", http.StripPrefix(pattern+"/", http.FileServer(http.Dir(logPath))))
}

func SetLogPath(path string) {
	logPath = path
	name := getLogFileName()
	os.MkdirAll(path, os.ModePerm)
	ShowConsole = true
	f, err := os.OpenFile(path+name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalln("%T err:%s", err, err)
	}
	log.SetOutput(f)
}
func E(a ...interface{}) {
	fmt.Fprintln(os.Stdout, a...)
}
func W(a ...interface{}) {
	log.Println(a)
	if ShowConsole {
		E(a)
	}
}
func Ef(format string, a ...interface{}) {
	fmt.Printf(format+"\n", a...)
}
func Wf(format string, a ...interface{}) {
	log.Printf(format, a...)
	if ShowConsole {
		Ef(format, a...)
	}

}
func ENullable(a interface{}) {
	if a != nil {
		E(a)
	}
}
func WNullable(a interface{}) {
	if a != nil {
		W(a)
	}
}

func Fatalf(format string, v ...interface{}) {
	log.Fatalln(fmt.Sprintf(format, v...))
	fmt.Sprintf(format, v...)
}
